<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="add(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type===1" size="small" type="text" @click="add(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="edit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 新增编辑的弹层 -->
        <el-dialog :visible="showDialog" :title="showTitle" @close="closeDialog">
          <!-- 表单内容 -->
          <el-form ref="form" label-width="100px" :model="formData" :rules="ruleForm">
            <el-form-item label="权限名称" prop="name">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="权限标识" prop="code">
              <el-input v-model="formData.code" />
            </el-form-item>
            <el-form-item label="权限描述" prop="description">
              <el-input v-model="formData.description" />
            </el-form-item>
            <el-form-item label="权限启用">
              <el-switch
                v-model="formData.enVisible"
                active-text="启用"
                active-value="1"
                inactive-text="不启用"
                inactive-value="0"
              />
            </el-form-item>
          </el-form>

          <template #footer>
            <div style="text-align: right;">
              <el-button @click="closeDialog">取消</el-button>
              <el-button type="primary" @click="clickSubmit">确定</el-button>
            </div>
          </template>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      ruleForm: {
        name: [{ required: true, message: '权限名称不能为空', trigger: ['blur', 'change'] }],
        code: [{ required: true, message: '权限表示不能为空', trigger: ['blur', 'change'] }],
        description: [{ required: true, message: '权限描述不能为空', trigger: ['blur', 'change'] }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑权限' : '添加权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data: res } = await reqGetPermissionList()
      // console.log(res)
      this.list = tranListToTreeData(res, '0')
    },
    add(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    clickSubmit() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        await reqAddPermission(this.formData)
        this.$message.success('操作成功')
        this.getPermissionList()
        this.showDialog = false
      })
    },
    async del(id) {
      const res = await this.$confirm('确定要删除吗？', '温馨提示').catch(error => error)
      if (res !== 'confirm') return
      // console.log(1)
      await reqDelPermission(id)
      this.getPermissionList()
    },
    async edit(id) {
      const { data: res } = await reqGetPermissionDetail(id)
      // console.log(res)
      this.formData = res
      this.showDialog = true
    },
    closeDialog() {
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
      this.$refs.form.resetFields()
      this.showDialog = false
    }
  }
}
</script>
